package cc.flyflow.core.listeners.impl;

import cc.flyflow.common.constants.ProcessInstanceConstant;
import cc.flyflow.common.dto.process.ProcessInstanceAssignUserRecordParamDto;
import cc.flyflow.common.utils.JsonUtil;
import cc.flyflow.core.listeners.EventListenerStrategy;
import cc.flyflow.core.utils.BizHttpUtil;
import cc.flyflow.core.utils.FlowableUtils;
import cn.hutool.core.convert.Convert;
import cn.hutool.extra.spring.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEntityEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.task.service.impl.persistence.entity.TaskEntityImpl;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 任务完成
 * <p>
 * 任务完成事件监听器
 *
 * @author Huijun Zhao;Jyl
 * @date 2023-10-10 10:12
 */
@Slf4j
@Component
public class TaskCompletedEventListener implements EventListenerStrategy, InitializingBean
{

    /**
     * 处理数据
     *
     * @param event
     * @return
     */
    @Override
    public void handle(FlowableEvent event)
    {
        TaskService taskService = SpringUtil.getBean(TaskService.class);
        RuntimeService runtimeService = SpringUtil.getBean(RuntimeService.class);

        //任务完成
        FlowableEntityEvent flowableEntityEvent = (FlowableEntityEvent) event;
        TaskEntityImpl task = (TaskEntityImpl) flowableEntityEvent.getEntity();

        String assignee = task.getAssignee();//执行人id
        String nodeId = task.getTaskDefinitionKey();//nodeid
        String processInstanceId = task.getProcessInstanceId();//实例id
        String processDefinitionId = task.getProcessDefinitionId();

        String tenantId = task.getTenantId();//流程id
        String flowId = FlowableUtils.getFlowId(processDefinitionId, tenantId);
        String taskType = task.getVariableLocal(ProcessInstanceConstant.VariableKey.TASK_TYPE, String.class);
        Map<String, Object> variables = taskService.getVariables(task.getId());
        //审批结果
        //true同意  false拒绝
        //判断下 如果是自动完成的 不处理
        Object autoCompleteObj = runtimeService.getVariableLocal(task.getExecutionId(),
                ProcessInstanceConstant.VariableKey.AUTO_COMPLETE_TASK);

        ProcessInstanceAssignUserRecordParamDto userRecordParamDto = new ProcessInstanceAssignUserRecordParamDto();
        userRecordParamDto.setFlowId(flowId);
        userRecordParamDto.setProcessInstanceId(processInstanceId);
        //   processNodeRecordAssignUserParamDto.setParentExecutionId();
        userRecordParamDto.setData(JsonUtil.toJSONString(variables));
        userRecordParamDto.setLocalData(JsonUtil.toJSONString(taskService.getVariablesLocal(task.getId())));

        userRecordParamDto.setNodeId(nodeId);
        userRecordParamDto.setUserId((assignee));
        userRecordParamDto.setTaskId(task.getId());
        userRecordParamDto.setNodeName(task.getName());
        userRecordParamDto.setFlowUniqueId(FlowableUtils.getFlowUniqueId(nodeId, flowId, processInstanceId, tenantId));

        userRecordParamDto.setTaskType(taskType);
        userRecordParamDto.setExecutionId(task.getExecutionId());
        userRecordParamDto.setTenantId(tenantId);
        userRecordParamDto.setAuto(Convert.toBool(autoCompleteObj, false));
        BizHttpUtil.taskCompletedEvent(userRecordParamDto);
    }

    @Override
    public void afterPropertiesSet() throws Exception
    {
        afterPropertiesSet(FlowableEngineEventType.TASK_COMPLETED.toString());
    }
}
